package com.iflytek.view;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.iflytek.utils.VoiceRecognitionManager;
import com.iflytek.voicedemo.R;

/**
 * 语音识别组件.
 *
 * @author YQS
 */

public class VoiceRecognitionView extends LinearLayout implements View.OnClickListener {

    private static String TAG = VoiceRecognitionView.class.getSimpleName();

    private Context mContext;
    private LayoutInflater inflater;

    private View rootView;
    private ImageView ivRecord;
    VoiceRecognitionManager voiceRecognitionManager;


    public VoiceRecognitionView(Context context) {
        this(context, null);
    }

    public VoiceRecognitionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoiceRecognitionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView(mContext);
        initComponent();
    }


    private void initView(Context mContext) {
        this.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this.setGravity(Gravity.CENTER);
        //获取语音布局
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.xunfei_layout_voice_recoginitionview, this, true);
        ivRecord = (ImageView) rootView.findViewById(R.id.iv_record);
        ivRecord.setOnClickListener(this);
    }

    /**
     * 初始化识别组件
     */
    private void initComponent() {
        voiceRecognitionManager = new VoiceRecognitionManager(mContext);
    }


    //释放
    public void release() {
        if (null != voiceRecognitionManager) {
            // 退出时释放连接
            voiceRecognitionManager.release();
        }
    }


    /**
     * 是否显示dialog
     *
     * @param show
     */
    public void setShowDialog(boolean show) {
        voiceRecognitionManager.setShowDialog(show);
    }

    /**
     * 是否翻译
     *
     * @param enable
     */
    public void setmTranslateEnable(boolean enable) {
        voiceRecognitionManager.setmTranslateEnable(enable);
    }
    public void setStartMuteOverTime(String startMuteOverTime) {
        voiceRecognitionManager.setStartMuteOverTime(startMuteOverTime);

    }


    public void setEndMuteOverTime(String endMuteOverTime) {
        voiceRecognitionManager.setEndMuteOverTime(endMuteOverTime);
    }

    /**
     * 设置返回结果有无标点
     * @param hasPunctuation
     */
    public void setHasPunctuation(boolean hasPunctuation) {
        voiceRecognitionManager.setHasPunctuation(hasPunctuation);


    }


    public void setSpeechRecognitionCallBack(VoiceRecognitionManager.SpeechRecognitionCallBack callBack) {
        voiceRecognitionManager.setVoiceRecognitionCallBack(callBack);
    }

    @Override
    public void onClick(View v) {
        voiceRecognitionManager.startRecognition();
    }



}
